<html>
<head><meta charset="utf-8"><title>MaybeUninit arrays · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html">MaybeUninit arrays</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171288038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit%20arrays/near/171288038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html#171288038">(Jul 19 2019 at 19:49)</a>:</h4>
<p>Im unsure what the difference between a <code>MaybeUninit&lt;[T; N]&gt;</code> and a <code>[MaybeUninit&lt;T&gt;; N]</code> is.<br>
What I understand is that the latter is preferred when dealing with partial arrays, however the ArrayVec crate uses the former, but also deals with partial arrays. (<a href="https://github.com/bluss/arrayvec/blob/master/src/lib.rs#L95" target="_blank" title="https://github.com/bluss/arrayvec/blob/master/src/lib.rs#L95">https://github.com/bluss/arrayvec/blob/master/src/lib.rs#L95</a>)</p>
<p>And, is this okey to do?:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">array</span>: <span class="nc">MaybeUninit</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">10</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="c1">// &lt;--- cast to *mut T, since the ptr() method gives *mut [T; 10]</span>
<span class="n">ptr</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">some_value</span><span class="p">)</span><span class="w"></span>
</pre></div>


<p>Notice the array within the MaybeUninit is never really initialized, but is still written to.</p>



<a name="171323071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit%20arrays/near/171323071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html#171323071">(Jul 20 2019 at 11:05)</a>:</h4>
<blockquote>
<p>Im unsure what the difference between a MaybeUninit&lt;[T; N]&gt; and a [MaybeUninit&lt;T&gt;; N] is.</p>
</blockquote>
<p>Good question! There isn't really any.</p>



<a name="171323073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit%20arrays/near/171323073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html#171323073">(Jul 20 2019 at 11:05)</a>:</h4>
<p>If we had const generics, we might want to add methods to freely convert between the two.</p>



<a name="171323077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit%20arrays/near/171323077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html#171323077">(Jul 20 2019 at 11:05)</a>:</h4>
<p>transmuting between the two is okay for any <code>T</code> and <code>N</code></p>



<a name="171323120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit%20arrays/near/171323120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/MaybeUninit.20arrays.html#171323120">(Jul 20 2019 at 11:06)</a>:</h4>
<blockquote>
<p>And, is this okey to do?</p>
</blockquote>
<p>yes. <code>ptr.write</code> on uninitialized memory is okay. having to do a raw ptr cast is not nice (IMO those should be frowned upon as much as <code>transmute</code>), but cannot always be avoided with our current APIs.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>